package com.cicigodd.brick.plug.generator.controller;

import com.cicigodd.brick.plug.annotation.resource.GetResource;
import com.cicigodd.brick.plug.common.response.Result;
import com.cicigodd.brick.plug.common.response.ResultGenerator;
import com.cicigodd.brick.plug.generator.service.GeneratorService;
import com.google.common.base.CaseFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 代码生成
 *
 * @author cicigodd
 * @date 2022/09/23 08:42:00
 */
@ApiIgnore
@RestController
public class GeneratorController {

    @Resource
    private GeneratorService generatorService;

    @GetResource(name = "获取表结构", path = "/getTableInfo" + "", requiredPermission = false)
    public Result getTableInfo(@RequestParam Map<String, Object> params) {
        String tableSchema = (String) params.get("tableSchema");
        String tableName = (String) params.get("tableName");
        List<Map<String, String>> tableInfo = generatorService.getTableInfo(tableSchema, tableName);
        return ResultGenerator.successResult(tableInfo);
    }

    /**
     * 代码生成
     */
//    @GetMapping("/v1/generator")
//    public Result generator(@RequestParam Map<String, Object> params) throws ConfigurationException {
//        String tableSchema = (String) params.get("tableSchema");
//        String tableName = (String) params.get("tableName");
//        String folder = (String) params.get("folder");
//        String moduleName = (String) params.get("moduleName");
//        String author = (String) params.get("author");
//        String system = (String) params.get("system");
//        List<Map<String, String>> tableInfo = generatorMapper.findTableInfo(tableSchema, tableName);
//        Configuration config = getConfig();
//        tableInfo.forEach(item -> {
//            item.put("java_type", config.getString(item.get("data_type")));
//            String javaName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, item.get("column_name").toLowerCase());
//            item.put("java_name", javaName);
//            item.put("java_func_name", javaName.substring(0, 1).toUpperCase() + javaName.substring(1));
//        });
//        generatorUtil.genCode(tableName, moduleName, folder, author, system, tableInfo);
//        return ResultGenerator.successResult();
//    }

    /**
     * 获取配置信息
     */
//    public Configuration getConfig() throws ConfigurationException {
//        return new PropertiesConfiguration("templates\\gen.properties");
//    }

}
